package com.hnucm.springboot.utils;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class MyUserService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //username登录页面用户输入的用户名
        //数据库user(id,username,password)  username select * from user where username = ? 0 1 多条
        //0条throws UsernameNotFoundException  1条 获取密码
        //roles(id,name)   user表 roles表多对多  中间表
        String password="123456";
        GrantedAuthority authority1 = new SimpleGrantedAuthority("user");
        GrantedAuthority authority2 = new SimpleGrantedAuthority("admin");
        ArrayList<GrantedAuthority> list = new ArrayList<>();
        list.add(authority1);
        list.add(authority2);
        return new User(username,new BCryptPasswordEncoder().encode(password),list);
    }
}
